package Comparable_etc;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/16 22:09
 * 说明：Comparable，比较类
 */

public class ComparableTest {
    @Test
    public void test1(){
        String[] arr = new String[]{"Tom", "Jerry", "Tony", "Rose", "Jack", "Lucy"};

        Arrays.sort(arr);

        for (String x:arr) {
            System.out.println(x);
        }
    }

    @Test
    public void test2(){

        Product p1 = new Product("A", 2);
        Product p2 = new Product("B", 546);
        Product p3 = new Product("C", -23);
        Product p4 = new Product("D", 456);
        Product p5 = new Product("E", 1);
        Product p6 = new Product("F", 11);
        Product[] arrP = new Product[]{p1, p2, p3, p4, p5, p6};
        // 类型转换异常
        // java.lang.ClassCastException: class Comparable_etc.Product cannot be cast to class java.lang.Comparable
        // Product不能转换为java.lang.Comparable
        // 需要Product类实现Comparable的接口

        // sort()方法的内部就是CompareTo()方法的调用，因此要在Product类中实现Comparable接口CompareTo()
        Arrays.sort(arrP);
        for (Product x : arrP) {
            System.out.println(x);
        }
    }
}
